home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / build_ext.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  18.8 KB  |  535 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.command.build_ext
  5.  
  6. Implements the Distutils 'build_ext' command, for building extension
  7. modules (currently limited to C extensions, should accommodate C++
  8. extensions ASAP)."""
  9. __revision__ = '$Id: build_ext.py 69317 2009-02-05 22:55:00Z tarek.ziade $'
  10. import sys
  11. import os
  12. import string
  13. import re
  14. from types import *
  15. from site import USER_BASE, USER_SITE
  16. from distutils.core import Command
  17. from distutils.errors import *
  18. from distutils.sysconfig import customize_compiler, get_python_version
  19. from distutils.dep_util import newer_group
  20. from distutils.extension import Extension
  21. from distutils.util import get_platform
  22. from distutils import log
  23. if os.name == 'nt':
  24.     from distutils.msvccompiler import get_build_version
  25.     MSVC_VERSION = int(get_build_version())
  26.  
  27. extension_name_re = re.compile('^[a-zA-Z_][a-zA-Z_0-9]*(\\.[a-zA-Z_][a-zA-Z_0-9]*)*$')
  28.  
  29. def show_compilers():
  30.     show_compilers = show_compilers
  31.     import distutils.ccompiler
  32.     show_compilers()
  33.  
  34.  
  35. class build_ext(Command):
  36.     description = 'build C/C++ extensions (compile/link to build directory)'
  37.     sep_by = " (separated by '%s')" % os.pathsep
  38.     user_options = [
  39.         ('build-lib=', 'b', 'directory for compiled extension modules'),
  40.         ('build-temp=', 't', 'directory for temporary files (build by-products)'),
  41.         ('plat-name=', 'p', 'platform name to cross-compile for, if supported (default: %s)' % get_platform()),
  42.         ('inplace', 'i', 'ignore build-lib and put compiled extensions into the source ' + 'directory alongside your pure Python modules'),
  43.         ('include-dirs=', 'I', 'list of directories to search for header files' + sep_by),
  44.         ('define=', 'D', 'C preprocessor macros to define'),
  45.         ('undef=', 'U', 'C preprocessor macros to undefine'),
  46.         ('libraries=', 'l', 'external C libraries to link with'),
  47.         ('library-dirs=', 'L', 'directories to search for external C libraries' + sep_by),
  48.         ('rpath=', 'R', 'directories to search for shared C libraries at runtime'),
  49.         ('link-objects=', 'O', 'extra explicit link objects to include in the link'),
  50.         ('debug', 'g', 'compile/link with debugging information'),
  51.         ('force', 'f', 'forcibly build everything (ignore file timestamps)'),
  52.         ('compiler=', 'c', 'specify the compiler type'),
  53.         ('swig-cpp', None, 'make SWIG create C++ files (default is C)'),
  54.         ('swig-opts=', None, 'list of SWIG command line options'),
  55.         ('swig=', None, 'path to the SWIG executable'),
  56.         ('user', None, 'add user include, library and rpath')]
  57.     boolean_options = [
  58.         'inplace',
  59.         'debug',
  60.         'force',
  61.         'swig-cpp',
  62.         'user']
  63.     help_options = [
  64.         ('help-compiler', None, 'list available compilers', show_compilers)]
  65.     
  66.     def initialize_options(self):
  67.         self.extensions = None
  68.         self.build_lib = None
  69.         self.plat_name = None
  70.         self.build_temp = None
  71.         self.inplace = 0
  72.         self.package = None
  73.         self.include_dirs = None
  74.         self.define = None
  75.         self.undef = None
  76.         self.libraries = None
  77.         self.library_dirs = None
  78.         self.rpath = None
  79.         self.link_objects = None
  80.         self.debug = None
  81.         self.force = None
  82.         self.compiler = None
  83.         self.swig = None
  84.         self.swig_cpp = None
  85.         self.swig_opts = None
  86.         self.user = None
  87.  
  88.     
  89.     def finalize_options(self):
  90.         sysconfig = sysconfig
  91.         import distutils
  92.         self.set_undefined_options('build', ('build_lib', 'build_lib'), ('build_temp', 'build_temp'), ('compiler', 'compiler'), ('debug', 'debug'), ('force', 'force'), ('plat_name', 'plat_name'))
  93.         if self.package is None:
  94.             self.package = self.distribution.ext_package
  95.         
  96.         self.extensions = self.distribution.ext_modules
  97.         py_include = sysconfig.get_python_inc()
  98.         plat_py_include = sysconfig.get_python_inc(plat_specific = 1)
  99.         if self.include_dirs is None:
  100.             if not self.distribution.include_dirs:
  101.                 pass
  102.             self.include_dirs = []
  103.         
  104.         if type(self.include_dirs) is StringType:
  105.             self.include_dirs = string.split(self.include_dirs, os.pathsep)
  106.         
  107.         self.include_dirs.append(py_include)
  108.         if plat_py_include != py_include:
  109.             self.include_dirs.append(plat_py_include)
  110.         
  111.         if type(self.libraries) is StringType:
  112.             self.libraries = [
  113.                 self.libraries]
  114.         
  115.         if self.libraries is None:
  116.             self.libraries = []
  117.         
  118.         if self.library_dirs is None:
  119.             self.library_dirs = []
  120.         elif type(self.library_dirs) is StringType:
  121.             self.library_dirs = string.split(self.library_dirs, os.pathsep)
  122.         
  123.         if self.rpath is None:
  124.             self.rpath = []
  125.         elif type(self.rpath) is StringType:
  126.             self.rpath = string.split(self.rpath, os.pathsep)
  127.         
  128.         if os.name == 'nt':
  129.             self.library_dirs.append(os.path.join(sys.exec_prefix, 'libs'))
  130.             if self.debug:
  131.                 self.build_temp = os.path.join(self.build_temp, 'Debug')
  132.             else:
  133.                 self.build_temp = os.path.join(self.build_temp, 'Release')
  134.             self.include_dirs.append(os.path.join(sys.exec_prefix, 'PC'))
  135.             if MSVC_VERSION == 9:
  136.                 if self.plat_name == 'win32':
  137.                     suffix = ''
  138.                 else:
  139.                     suffix = self.plat_name[4:]
  140.                 new_lib = os.path.join(sys.exec_prefix, 'PCbuild')
  141.                 if suffix:
  142.                     new_lib = os.path.join(new_lib, suffix)
  143.                 
  144.                 self.library_dirs.append(new_lib)
  145.             elif MSVC_VERSION == 8:
  146.                 self.library_dirs.append(os.path.join(sys.exec_prefix, 'PC', 'VS8.0', 'win32release'))
  147.             elif MSVC_VERSION == 7:
  148.                 self.library_dirs.append(os.path.join(sys.exec_prefix, 'PC', 'VS7.1'))
  149.             else:
  150.                 self.library_dirs.append(os.path.join(sys.exec_prefix, 'PC', 'VC6'))
  151.         
  152.         if os.name == 'os2':
  153.             self.library_dirs.append(os.path.join(sys.exec_prefix, 'Config'))
  154.         
  155.         if sys.platform[:6] == 'cygwin' or sys.platform[:6] == 'atheos':
  156.             if sys.executable.startswith(os.path.join(sys.exec_prefix, 'bin')):
  157.                 self.library_dirs.append(os.path.join(sys.prefix, 'lib', 'python' + get_python_version(), 'config'))
  158.             else:
  159.                 self.library_dirs.append('.')
  160.         
  161.         sysconfig.get_config_var('Py_ENABLE_SHARED')
  162.         if (sys.platform.startswith('linux') and sys.platform.startswith('gnu') or sys.platform.startswith('sunos')) and sysconfig.get_config_var('Py_ENABLE_SHARED'):
  163.             if sys.executable.startswith(os.path.join(sys.exec_prefix, 'bin')):
  164.                 self.library_dirs.append(sysconfig.get_config_var('LIBDIR'))
  165.             else:
  166.                 self.library_dirs.append('.')
  167.         
  168.         if self.define:
  169.             defines = string.split(self.define, ',')
  170.             self.define = map((lambda symbol: (symbol, '1')), defines)
  171.         
  172.         if self.undef:
  173.             self.undef = string.split(self.undef, ',')
  174.         
  175.         if self.swig_opts is None:
  176.             self.swig_opts = []
  177.         else:
  178.             self.swig_opts = self.swig_opts.split(' ')
  179.         if self.user:
  180.             user_include = os.path.join(USER_BASE, 'include')
  181.             user_lib = os.path.join(USER_BASE, 'lib')
  182.             if os.path.isdir(user_include):
  183.                 self.include_dirs.append(user_include)
  184.             
  185.             if os.path.isdir(user_lib):
  186.                 self.library_dirs.append(user_lib)
  187.                 self.rpath.append(user_lib)
  188.             
  189.         
  190.  
  191.     
  192.     def run(self):
  193.         new_compiler = new_compiler
  194.         import distutils.ccompiler
  195.         if not self.extensions:
  196.             return None
  197.         if self.distribution.has_c_libraries():
  198.             build_clib = self.get_finalized_command('build_clib')
  199.             if not build_clib.get_library_names():
  200.                 pass
  201.             self.libraries.extend([])
  202.             self.library_dirs.append(build_clib.build_clib)
  203.         
  204.         self.compiler = new_compiler(compiler = self.compiler, verbose = self.verbose, dry_run = self.dry_run, force = self.force)
  205.         customize_compiler(self.compiler)
  206.         if os.name == 'nt' and self.plat_name != get_platform():
  207.             self.compiler.initialize(self.plat_name)
  208.         
  209.         if self.include_dirs is not None:
  210.             self.compiler.set_include_dirs(self.include_dirs)
  211.         
  212.         if self.define is not None:
  213.             for name, value in self.define:
  214.                 self.compiler.define_macro(name, value)
  215.             
  216.         
  217.         if self.undef is not None:
  218.             for macro in self.undef:
  219.                 self.compiler.undefine_macro(macro)
  220.             
  221.         
  222.         if self.libraries is not None:
  223.             self.compiler.set_libraries(self.libraries)
  224.         
  225.         if self.library_dirs is not None:
  226.             self.compiler.set_library_dirs(self.library_dirs)
  227.         
  228.         if self.rpath is not None:
  229.             self.compiler.set_runtime_library_dirs(self.rpath)
  230.         
  231.         if self.link_objects is not None:
  232.             self.compiler.set_link_objects(self.link_objects)
  233.         
  234.         self.build_extensions()
  235.  
  236.     
  237.     def check_extensions_list(self, extensions):
  238.         """Ensure that the list of extensions (presumably provided as a
  239.         command option 'extensions') is valid, i.e. it is a list of
  240.         Extension objects.  We also support the old-style list of 2-tuples,
  241.         where the tuples are (ext_name, build_info), which are converted to
  242.         Extension instances here.
  243.  
  244.         Raise DistutilsSetupError if the structure is invalid anywhere;
  245.         just returns otherwise.
  246.         """
  247.         if type(extensions) is not ListType:
  248.             raise DistutilsSetupError, "'ext_modules' option must be a list of Extension instances"
  249.         type(extensions) is not ListType
  250.         for i in range(len(extensions)):
  251.             ext = extensions[i]
  252.             if isinstance(ext, Extension):
  253.                 continue
  254.             
  255.             (ext_name, build_info) = ext
  256.             log.warn("old-style (ext_name, build_info) tuple found in ext_modules for extension '%s'-- please convert to Extension instance" % ext_name)
  257.             if type(ext) is not TupleType and len(ext) != 2:
  258.                 raise DistutilsSetupError, "each element of 'ext_modules' option must be an Extension instance or 2-tuple"
  259.             len(ext) != 2
  260.             if not type(ext_name) is StringType and extension_name_re.match(ext_name):
  261.                 raise DistutilsSetupError, "first element of each tuple in 'ext_modules' must be the extension name (a string)"
  262.             extension_name_re.match(ext_name)
  263.             if type(build_info) is not DictionaryType:
  264.                 raise DistutilsSetupError, "second element of each tuple in 'ext_modules' must be a dictionary (build info)"
  265.             type(build_info) is not DictionaryType
  266.             ext = Extension(ext_name, build_info['sources'])
  267.             for key in ('include_dirs', 'library_dirs', 'libraries', 'extra_objects', 'extra_compile_args', 'extra_link_args'):
  268.                 val = build_info.get(key)
  269.                 if val is not None:
  270.                     setattr(ext, key, val)
  271.                     continue
  272.             
  273.             ext.runtime_library_dirs = build_info.get('rpath')
  274.             if 'def_file' in build_info:
  275.                 log.warn("'def_file' element of build info dict no longer supported")
  276.             
  277.             macros = build_info.get('macros')
  278.             if macros:
  279.                 ext.define_macros = []
  280.                 ext.undef_macros = []
  281.                 for macro in macros:
  282.                     if type(macro) is TupleType:
  283.                         if len(macro) <= len(macro):
  284.                             pass
  285.                         elif not len(macro) <= 2:
  286.                             raise DistutilsSetupError, "'macros' element of build info dict must be 1- or 2-tuple"
  287.                         
  288.                     if len(macro) == 1:
  289.                         ext.undef_macros.append(macro[0])
  290.                         continue
  291.                     1
  292.                     if len(macro) == 2:
  293.                         ext.define_macros.append(macro)
  294.                         continue
  295.                 
  296.             
  297.             extensions[i] = ext
  298.         
  299.  
  300.     
  301.     def get_source_files(self):
  302.         self.check_extensions_list(self.extensions)
  303.         filenames = []
  304.         for ext in self.extensions:
  305.             filenames.extend(ext.sources)
  306.         
  307.         return filenames
  308.  
  309.     
  310.     def get_outputs(self):
  311.         self.check_extensions_list(self.extensions)
  312.         outputs = []
  313.         for ext in self.extensions:
  314.             fullname = self.get_ext_fullname(ext.name)
  315.             outputs.append(os.path.join(self.build_lib, self.get_ext_filename(fullname)))
  316.         
  317.         return outputs
  318.  
  319.     
  320.     def build_extensions(self):
  321.         self.check_extensions_list(self.extensions)
  322.         for ext in self.extensions:
  323.             self.build_extension(ext)
  324.         
  325.  
  326.     
  327.     def build_extension(self, ext):
  328.         sources = ext.sources
  329.         if sources is None or type(sources) not in (ListType, TupleType):
  330.             raise DistutilsSetupError, ("in 'ext_modules' option (extension '%s'), " + "'sources' must be present and must be " + 'a list of source filenames') % ext.name
  331.         type(sources) not in (ListType, TupleType)
  332.         sources = list(sources)
  333.         fullname = self.get_ext_fullname(ext.name)
  334.         if self.inplace:
  335.             modpath = string.split(fullname, '.')
  336.             package = string.join(modpath[0:-1], '.')
  337.             base = modpath[-1]
  338.             build_py = self.get_finalized_command('build_py')
  339.             package_dir = build_py.get_package_dir(package)
  340.             ext_filename = os.path.join(package_dir, self.get_ext_filename(base))
  341.         else:
  342.             ext_filename = os.path.join(self.build_lib, self.get_ext_filename(fullname))
  343.         depends = sources + ext.depends
  344.         if not self.force or newer_group(depends, ext_filename, 'newer'):
  345.             log.debug("skipping '%s' extension (up-to-date)", ext.name)
  346.             return None
  347.         log.info("building '%s' extension", ext.name)
  348.         sources = self.swig_sources(sources, ext)
  349.         if not ext.extra_compile_args:
  350.             pass
  351.         extra_args = []
  352.         macros = ext.define_macros[:]
  353.         for undef in ext.undef_macros:
  354.             macros.append((undef,))
  355.         
  356.         objects = self.compiler.compile(sources, output_dir = self.build_temp, macros = macros, include_dirs = ext.include_dirs, debug = self.debug, extra_postargs = extra_args, depends = ext.depends)
  357.         self._built_objects = objects[:]
  358.         if ext.extra_objects:
  359.             objects.extend(ext.extra_objects)
  360.         
  361.         if not ext.extra_link_args:
  362.             pass
  363.         extra_args = []
  364.         if not ext.language:
  365.             pass
  366.         language = self.compiler.detect_language(sources)
  367.         self.compiler.link_shared_object(objects, ext_filename, libraries = self.get_libraries(ext), library_dirs = ext.library_dirs, runtime_library_dirs = ext.runtime_library_dirs, extra_postargs = extra_args, export_symbols = self.get_export_symbols(ext), debug = self.debug, build_temp = self.build_temp, target_lang = language)
  368.  
  369.     
  370.     def swig_sources(self, sources, extension):
  371.         """Walk the list of source files in 'sources', looking for SWIG
  372.         interface (.i) files.  Run SWIG on all that are found, and
  373.         return a modified 'sources' list with SWIG source files replaced
  374.         by the generated C (or C++) files.
  375.         """
  376.         new_sources = []
  377.         swig_sources = []
  378.         swig_targets = { }
  379.         if self.swig_cpp:
  380.             log.warn('--swig-cpp is deprecated - use --swig-opts=-c++')
  381.         
  382.         if self.swig_cpp and '-c++' in self.swig_opts or '-c++' in extension.swig_opts:
  383.             target_ext = '.cpp'
  384.         else:
  385.             target_ext = '.c'
  386.         for source in sources:
  387.             (base, ext) = os.path.splitext(source)
  388.             if ext == '.i':
  389.                 new_sources.append(base + '_wrap' + target_ext)
  390.                 swig_sources.append(source)
  391.                 swig_targets[source] = new_sources[-1]
  392.                 continue
  393.             new_sources.append(source)
  394.         
  395.         if not swig_sources:
  396.             return new_sources
  397.         if not self.swig:
  398.             pass
  399.         swig = self.find_swig()
  400.         swig_cmd = [
  401.             swig,
  402.             '-python']
  403.         swig_cmd.extend(self.swig_opts)
  404.         if self.swig_cpp:
  405.             swig_cmd.append('-c++')
  406.         
  407.         if not self.swig_opts:
  408.             for o in extension.swig_opts:
  409.                 swig_cmd.append(o)
  410.             
  411.         
  412.         for source in swig_sources:
  413.             target = swig_targets[source]
  414.             log.info('swigging %s to %s', source, target)
  415.             self.spawn(swig_cmd + [
  416.                 '-o',
  417.                 target,
  418.                 source])
  419.         
  420.         return new_sources
  421.  
  422.     
  423.     def find_swig(self):
  424.         '''Return the name of the SWIG executable.  On Unix, this is
  425.         just "swig" -- it should be in the PATH.  Tries a bit harder on
  426.         Windows.
  427.         '''
  428.         if os.name == 'posix':
  429.             return 'swig'
  430.         if os.name == 'nt':
  431.             for vers in ('1.3', '1.2', '1.1'):
  432.                 fn = os.path.join('c:\\swig%s' % vers, 'swig.exe')
  433.                 if os.path.isfile(fn):
  434.                     return fn
  435.             else:
  436.                 return 'swig.exe'
  437.         os.path.isfile(fn)
  438.         if os.name == 'os2':
  439.             return 'swig.exe'
  440.         raise DistutilsPlatformError, "I don't know how to find (much less run) SWIG on platform '%s'" % os.name
  441.  
  442.     
  443.     def get_ext_fullname(self, ext_name):
  444.         if self.package is None:
  445.             return ext_name
  446.         return self.package + '.' + ext_name
  447.  
  448.     
  449.     def get_ext_filename(self, ext_name):
  450.         '''Convert the name of an extension (eg. "foo.bar") into the name
  451.         of the file from which it will be loaded (eg. "foo/bar.so", or
  452.         "foo\\bar.pyd").
  453.         '''
  454.         get_config_var = get_config_var
  455.         import distutils.sysconfig
  456.         ext_path = string.split(ext_name, '.')
  457.         if os.name == 'os2':
  458.             ext_path[len(ext_path) - 1] = ext_path[len(ext_path) - 1][:8]
  459.         
  460.         so_ext = get_config_var('SO')
  461.         if os.name == 'nt' and self.debug:
  462.             return apply(os.path.join, ext_path) + '_d' + so_ext
  463.         return os.path.join(*ext_path) + so_ext
  464.  
  465.     
  466.     def get_export_symbols(self, ext):
  467.         '''Return the list of symbols that a shared extension has to
  468.         export.  This either uses \'ext.export_symbols\' or, if it\'s not
  469.         provided, "init" + module_name.  Only relevant on Windows, where
  470.         the .pyd file (DLL) must export the module "init" function.
  471.         '''
  472.         initfunc_name = 'init' + string.split(ext.name, '.')[-1]
  473.         if initfunc_name not in ext.export_symbols:
  474.             ext.export_symbols.append(initfunc_name)
  475.         
  476.         return ext.export_symbols
  477.  
  478.     
  479.     def get_libraries(self, ext):
  480.         """Return the list of libraries to link against when building a
  481.         shared extension.  On most platforms, this is just 'ext.libraries';
  482.         on Windows and OS/2, we add the Python library (eg. python20.dll).
  483.         """
  484.         if sys.platform == 'win32':
  485.             MSVCCompiler = MSVCCompiler
  486.             import distutils.msvccompiler
  487.             if not isinstance(self.compiler, MSVCCompiler):
  488.                 template = 'python%d%d'
  489.                 if self.debug:
  490.                     template = template + '_d'
  491.                 
  492.                 pythonlib = template % (sys.hexversion >> 24, sys.hexversion >> 16 & 255)
  493.                 return ext.libraries + [
  494.                     pythonlib]
  495.             return ext.libraries
  496.         sys.platform == 'win32'
  497.         if sys.platform == 'os2emx':
  498.             template = 'python%d%d'
  499.             pythonlib = template % (sys.hexversion >> 24, sys.hexversion >> 16 & 255)
  500.             return ext.libraries + [
  501.                 pythonlib]
  502.         if sys.platform[:6] == 'cygwin':
  503.             template = 'python%d.%d'
  504.             pythonlib = template % (sys.hexversion >> 24, sys.hexversion >> 16 & 255)
  505.             return ext.libraries + [
  506.                 pythonlib]
  507.         if sys.platform[:6] == 'atheos':
  508.             sysconfig = sysconfig
  509.             import distutils
  510.             template = 'python%d.%d'
  511.             pythonlib = template % (sys.hexversion >> 24, sys.hexversion >> 16 & 255)
  512.             extra = []
  513.             for lib in sysconfig.get_config_var('SHLIBS').split():
  514.                 if lib.startswith('-l'):
  515.                     extra.append(lib[2:])
  516.                     continue
  517.                 sys.platform[:6] == 'cygwin'
  518.                 extra.append(lib)
  519.             
  520.             return ext.libraries + [
  521.                 pythonlib,
  522.                 'm'] + extra
  523.         if sys.platform == 'darwin':
  524.             return ext.libraries
  525.         sysconfig = sysconfig
  526.         import distutils
  527.         if sysconfig.get_config_var('Py_ENABLE_SHARED'):
  528.             template = 'python%d.%d'
  529.             pythonlib = template % (sys.hexversion >> 24, sys.hexversion >> 16 & 255)
  530.             return ext.libraries + [
  531.                 pythonlib]
  532.         return ext.libraries
  533.  
  534.  
  535.